package interview;

import java.util.concurrent.Semaphore;

class MyPrint {
    private int count = 1;
    private Semaphore a = new Semaphore(1);
    private Semaphore b = new Semaphore(0);

    public void printOdd () {
        try {
            System.out.println("Odd Hello");
            while (count <= 10) {
                a.acquire();
                if (count <= 10)
                    System.out.println(Thread.currentThread().getName() + "\t"
                        + count);
                count++;
                b.release();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void printEven () {
        try {
            System.out.println("Even Hello");
            while (count <= 10) {
                b.acquire();
                System.out.println(Thread.currentThread().getName() + "\t"
                        + count);
                count++;
                a.release();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

public class TakeTurnPrintOneToHundred {
    public static void main(String[] args) {
        MyPrint myPrint = new MyPrint();
        new Thread(()->{
            myPrint.printOdd();
        }, "O").start();

        new Thread(()->{
            myPrint.printEven();
        }, "E").start();
    }
}
